import Vue from 'vue'
import Vuex from 'vuex'

import { getWeChatUser } from "@/api/groups"

Vue.use(Vuex)


export default new Vuex.Store({
  state: {
    groups_id: 0,

  },
  mutations: {

    SET_GROUPS_ID: (state, id) => {
      state.groups_id = id
    },
  },
  actions: {
    setGroupsId({ commit }, id) {
      commit("SET_GROUPS_ID", id)
    },

    getUserInfo({ commit }) {
      return new Promise((resolve, reject) => {
        const openid = localStorage.getItem("openid");
        getWeChatUser(openid).then(res => {
          if (!res) {
            return reject('读取用户信息数据失败.')
          }
          const { groups } = res.data
          commit("SET_GROUPS_ID", groups)

          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

  },
  getters: {
    groups_id: state => state.groups_id,

  }
})